//
//  ChooseScene.m
//  GKBHouse
//
//  Created by  on 12-7-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChooseScene.h"
//#import "GKBNavigationBar.h"
//#import "GKBMainController.h"
//#import "SceneSetting.h"
#import "Global1.h"
@implementation SceneButton
@synthesize sceneID;
@synthesize name;
@synthesize OpenEqus;
- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    self.OpenEqus = [NSMutableArray array];
    return self;
}
- (void)dealloc
{
    self.sceneID = nil;
    self.name = nil;
    self.OpenEqus = nil;
    [super dealloc];
}
@end

@implementation ChooseScene
@synthesize sceneArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self initTopBar];
    [self initButton];
}
- (void)viewWillAppear:(BOOL)animated
{
   [self initScene]; 
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
//}
- (void)initTopBar
{    
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(dismissView) withTitle:NSLocalizedString(@"sceneSetting", nil)];
    [self.view addSubview:topBar];    
    [topBar release];
    
}
- (void)dismissView
{
    [self dismissModalViewControllerAnimated:NO];
}
- (void)initButton
{
//    NSString *scene = NSLocalizedString(@"scene", nil);
//    int i = 1;
//    NSString *title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene1 setTitle:title forState:UIControlStateNormal];
//    [scene1 addTarget:self action:@selector(openSceneSetting:) forControlEvents:UIControlEventTouchUpInside];
//    title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene2 setTitle:title forState:UIControlStateNormal];
//    title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene3 setTitle:title forState:UIControlStateNormal];
//    title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene4 setTitle:title forState:UIControlStateNormal];
//    title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene5 setTitle:title forState:UIControlStateNormal];
//    title = [scene stringByAppendingFormat:@"%d",i++];
//    [scene6 setTitle:title forState:UIControlStateNormal];
    
    NSString *scene = NSLocalizedString(@"scene", nil);
    self.sceneArray = [NSArray arrayWithObjects:scene1, scene2, scene3, scene4, scene5, scene6, nil];
    
    for (int i=1; i<7; i++) {
        NSString *scenId = [NSString stringWithFormat:@"%d", i];
        NSString *title = [scene stringByAppendingFormat:@"%d",i];
        SceneButton *scene = [self.sceneArray objectAtIndex:i-1];
        scene.sceneID = scenId;
        
        [scene setTitle:title forState:UIControlStateNormal];
        [scene addTarget:self action:@selector(openSceneSetting:) forControlEvents:UIControlEventTouchUpInside];
        
    }
}
- (void)initScene
{
    GKBMainController *main = [GKBMainController mainCtr];

    for (NewEquipment *equ in main.sceneArray) {
        NSInteger sceneId = [equ.switchId integerValue];
        if (sceneId>0) {
            SceneButton *sceneBut = [self.sceneArray objectAtIndex:sceneId - 1];
            NSString *name = equ.name;
            NSMutableArray *openId = equ.openEques;
            [sceneBut setTitle:name forState:UIControlStateNormal];
            [sceneBut setName:name];
            sceneBut.OpenEqus = openId;
        }
        
    }
    
}
- (void)setScene
{
    NSString *sceneFile = [AppFile sceneFile];
    NSMutableDictionary *allScene = [NSMutableDictionary dictionaryWithContentsOfFile:sceneFile];
    
    if (allScene) {
        for (int i = 1; i<7; i++) {
            NSString *key = [NSString stringWithFormat:@"%d",i];
            NSMutableDictionary *scene = [allScene objectForKey:key];
            if (scene) {
                NSString *name = [scene objectForKey:@"name"];
                NSMutableArray *openId = [scene objectForKey:@"OpenEqus"];
                SceneButton *sceneBut = [self.sceneArray objectAtIndex:i-1];
                [sceneBut setTitle:name forState:UIControlStateNormal];
                [sceneBut setName:name];
                sceneBut.OpenEqus = openId;
            }
            
        }
        
    }
   
}
- (void)openSceneSetting:(SceneButton *)but
{
//    [self dismissView];
    SceneSetting *sceneSetting = [[SceneSetting alloc]init];
    sceneSetting.sceneID = but.sceneID;
    sceneSetting.sceneName = but.titleLabel.text;
    sceneSetting.OpenEqus = but.OpenEqus;
    [self presentModalViewController:sceneSetting animated:NO];
    [sceneSetting release];
}
@end


